package com.cskaoyan.market.admin.service.impl;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.SqlSession;

import com.cskaoyan.market.admin.service.AdminTopicService;
import com.cskaoyan.market.db.domain.MarketGoods;
import com.cskaoyan.market.db.domain.MarketTopic;
import com.cskaoyan.market.db.domain.MarketTopicExample;
import com.cskaoyan.market.db.mapper.MarketGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketTopicMapper;
import com.cskaoyan.market.util.MyBatisUtil;
import com.github.pagehelper.PageHelper;

/**
 * @author BlackFlame33
 * @date 2023/03/22
 **/
public class AdminTopicServiceImpl implements AdminTopicService {

    @Override
    public List<MarketTopic> getTopicList(int page, int limit, String sort, String order) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        MarketTopicExample example = new MarketTopicExample();
        example.setOrderByClause(sort + " " + order);
        example.or().andDeletedEqualTo(false);
        PageHelper.startPage(page, limit);
        List<MarketTopic> marketTopics = mapper.selectByExample(example);
        session.commit();
        session.close();
        return marketTopics;
    }

    @Override
    public MarketTopic createTopic(Integer[] goods, String title, String subtitle, String picUrl, String content,
        Integer price, String readCount) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        MarketTopic marketTopic = new MarketTopic();
        marketTopic.setTitle(title);
        marketTopic.setSubtitle(subtitle);
        marketTopic.setContent(content);
        marketTopic.setPrice(BigDecimal.valueOf(price));
        marketTopic.setReadCount(readCount);
        marketTopic.setPicUrl(picUrl);
        marketTopic.setGoods(goods);
        marketTopic.setAddTime(LocalDateTime.now());
        marketTopic.setUpdateTime(LocalDateTime.now());
        marketTopic.setDeleted(false);
        mapper.insert(marketTopic);
        session.commit();
        session.close();
        return marketTopic;
    }

    @Override
    public MarketTopic updateTopic(MarketTopic marketTopic) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        mapper.updateByPrimaryKey(marketTopic);
        session.commit();
        session.close();
        return marketTopic;
    }

    @Override
    public Map<String, Object> readTopic(int topicId) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper topicMapper = session.getMapper(MarketTopicMapper.class);
        MarketGoodsMapper goodsMapper = session.getMapper(MarketGoodsMapper.class);
        Map<String, Object> data = new HashMap<>();
        List<Map<String, Object>> goodsList = new LinkedList<>();
        Map<String, Object> goodMap = new HashMap<>();
        MarketTopic topic = topicMapper.selectByPrimaryKey(topicId);
        Integer[] goods = topic.getGoods();
        for (Integer good : goods) {
            MarketGoods marketGoods = goodsMapper.selectByPrimaryKey(good);
            goodMap.put("id", marketGoods.getId());
            goodMap.put("name", marketGoods.getName());
            goodMap.put("brief", marketGoods.getBrief());
            goodMap.put("picUrl", marketGoods.getPicUrl());
            goodMap.put("isNew", marketGoods.getIsNew());
            goodMap.put("isHot", marketGoods.getIsHot());
            goodMap.put("counterPrice", marketGoods.getCounterPrice());
            goodMap.put("retailPrice", marketGoods.getRetailPrice());
            goodsList.add(goodMap);
        }
        data.put("goodsList", goodsList);
        data.put("topic", topic);
        return data;
    }

    @Override
    public int deleteTopic(MarketTopic marketTopic) {
        SqlSession session = MyBatisUtil.getSession();
        MarketTopicMapper mapper = session.getMapper(MarketTopicMapper.class);
        marketTopic.setDeleted(true);
        int code = mapper.updateByPrimaryKey(marketTopic);
        session.commit();
        session.close();
        return code;
    }
}